package cn.hftoday.core;

import java.util.*;
import org.apache.log4j.*;
import cn.hftoday.db.*;
import cn.hftoday.util.*;
import cn.hftoday.org.*;

/**
 * Conversation
 * @author lane.cn@gmail.com
 *
 */
public class Conversation {
	private static final Logger LOG = Logger.getLogger(Conversation.class);
	private String id;
	private String accountId;
	private Calendar createTime = CalendarParser.INVALID_TIME;
	private Calendar updateTime = CalendarParser.INVALID_TIME;
	private int code;
	private boolean remember = false;

	/**
	 * Session id
	 * @return
	 */
	public String getId() {
		return id;
	}

	/**
	 * Session id
	 * @param id
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * create time
	 * @return
	 */
	public Calendar getCreateTime() {
		return createTime;
	}

	/**
	 * create time
	 * @param createTime
	 */
	public void setCreateTime(Calendar createTime) {
		this.createTime = createTime;
	}

	/**
	 * update time
	 * @return
	 */
	public Calendar getUpdateTime() {
		return updateTime;
	}

	/**
	 * update time
	 * @param updateTime
	 */
	public void setUpdateTime(Calendar updateTime) {
		this.updateTime = updateTime;
	}

	/**
	 * last package code
	 * @return
	 */
	public int getCode() {
		return code;
	}

	/**
	 * last package code
	 * @param code
	 */
	public void setCode(int code) {
		this.code = code;
	}

	/**
	 * remember conversation
	 * @return
	 */
	public boolean isRemember() {
		return remember;
	}

	/**
	 * remember conversation
	 * @param remember
	 */
	public void setRemember(boolean remember) {
		this.remember = remember;
	}
	
	/**
	 * related account
	 * @return
	 */
	public Account getAccount() {
		return (Account)Mapper.getInstance().queryForObject("SelectAccountById", accountId);
	}
	
	/**
	 * related account
	 * @param remember
	 */
	public void setAccount(Account account) {
		if (account == null) {
			accountId = null;
		}
		else {
			accountId = account.getId();
		}
	}

	private static void _select(Object object, RecordSet rs, int i) {
		Conversation o = (Conversation)object;
		o.id = rs.getItem(i, "f_id");
		o.accountId = rs.getItem(i, "f_account");
		o.createTime = CalendarParser.toCalendar(rs.getItem(i, "f_createtime"));
		o.updateTime = CalendarParser.toCalendar(rs.getItem(i, "f_updatetime"));
		o.code = Integer.parseInt(rs.getItem(i, "f_code"));
		o.remember = rs.getItem(i, "f_remember").equals("1");
	}
	
	private static String _insert(Object object, String sql) {
		Conversation o = (Conversation)object;
		return StringParser.formatSql(sql, 
			o.id, 
			o.accountId, 
			CalendarParser.toString(o.createTime), 
			CalendarParser.toString(o.updateTime), 
			"" + o.code, 
			o.remember ? "1" : "0");
	}
	
	private static String _update(Object object, String sql) {
		Conversation o = (Conversation)object;
		return StringParser.formatSql(sql,
			o.id,
			o.accountId,
			CalendarParser.toString(o.updateTime),
			"" + o.code,
			(o.remember ? "1" : "0"));
	}
	
	private static String _delete(Object object, String sql) {
		Conversation o = (Conversation)object;
		return StringParser.formatSql(sql, 
			o.id);
	}
}
